package com.cloudkinto.service.dic.impl;

import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.utils.snow.SnowflakeIdUtil;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.entity.DicDo;
import com.cloudkinto.dao.DicDao;
import com.cloudkinto.service.dic.DicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.service.dic.vo.dic.DicAddReq;
import com.cloudkinto.service.dic.vo.dic.DicDetailRes;
import com.cloudkinto.service.dic.vo.dic.DicListPageRes;
import com.cloudkinto.service.dic.vo.dic.DicUpdateReq;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 系统码表 service实现类
 * </p>
 *
 * @author 张永远
 * @since 2020-05-19
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class DicServiceImpl extends ServiceImpl<DicDao, DicDo> implements DicService {
    @Autowired
    private DicDao dao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public DicAddReq add(DicAddReq req, Long userId) throws BizException {
        addCheck(req);
        DicDo entityDo = new DicDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return req;
    }

    //新增校验
    private void addCheck(DicAddReq req) throws BizException {
        QueryWrapper<DicDo> wrapper = new QueryWrapper<>();
        wrapper.eq("dic_name", req.getDicName());
        int count = dao.selectCount(wrapper);
        if (count > 0) {
            throw new BizException("名称重复！");
        }
    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(DicDo entityDo, Long userId) {
    }


    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success(detail(id));
    }

    @Override
    public DicUpdateReq update(DicUpdateReq req, Long userId) throws BizException {
        updateCheck(req);
        DicDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);
    }

    //编辑校验
    private void updateCheck(DicUpdateReq req) throws BizException {
        QueryWrapper<DicDo> wrapper = new QueryWrapper<>();
        int count = dao.selectCount(wrapper.eq("dic_name", req.getDicName()).ne("id", req.getId()));
        if (count > 0) {
            throw new BizException("名称重复！");
        }
    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(DicDo entityDo, Long userId) {
    }

    //做修改之后要执行的业务定制处理
    private DicUpdateReq handlerUpdateAfter(DicDo entityDo) {
        DicUpdateReq res = new DicUpdateReq();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        DicDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(DicDo entityDo, Long userId) {
    }

    @Override
    public DicDetailRes detail(Long id) {
        DicDo entityDo = dao.selectById(id);
        DicDetailRes res = new DicDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private DicDetailRes handlerDetailAfter(DicDetailRes res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<DicListPageRes> getList(Map<String, String> map) {
        QueryWrapper<DicDo> wrapper = queryBuild(map);
        List<DicDo> list = this.list(wrapper);
        List<DicListPageRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<DicDo> wrapper = queryBuild(map);
        IPage<DicDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<DicListPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<DicListPageRes> handlerListAfter(List<DicDo> list) {
        List<DicListPageRes> result = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            result = list.stream().map(dicDo -> {
                DicListPageRes res = new DicListPageRes();
                BeanUtils.copyProperties(dicDo, res);
                return res;
            }).collect(Collectors.toList());
        }
        return result;
    }

    private IPage<DicDo> pageInit(Map<String, String> map) {
        IPage<DicDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<DicDo> queryBuild(Map<String, String> map) {
        QueryWrapper<DicDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("dicName"))) {
            wrapper.like("dic_name", map.get("dicName"));
        }
        return wrapper;
    }


}
